<?php

namespace app\admin\controller;

use \think\Controller;
use think\Db;
use app\admin\model\TobDiscount as TobDiscountModel;

class Tob extends Controller
{

    //toB折扣表格
    public function index()
    {
        $info = db("tob_discount")
            ->where("type",0)
            ->field("*,concat(ctypeid1,'_',ctypeid2) as ctypeid_str")
            ->order("id asc")
            ->select();
        $ctypeid_arr = [];
        if(!empty($info)){
            $ctypeid_arr = array_column($info,"ctypeid_str");
        }

        $ctype1 = 5;//toB标品品类   第一级分类id
        $ctype2 = 6;//toB标品品类   第一级分类id
        $tagData1 = db("tag")
            ->where("tag_type",$ctype1)
            ->where("tag_status",1)
            ->field("tag_id,tag_name")
            ->select();
        if(!empty($tagData1)){
            foreach ($tagData1 as $key=>$value) {
                $tagData2 = db("tag")
                    ->where("tag_type",$ctype2)
                    ->where("tag_status",1)
                    ->field("tag_id as cid2,tag_name as cname2")
                    ->select();
                foreach ($tagData2 as $k=>$v) {
                    $ctypeid_str = $value['tag_id']."_".$v['cid2'];
                    if(!in_array($ctypeid_str,$ctypeid_arr)){
                        $insertData = [
                            'title'=>$value['tag_name']."-".$v['cname2'],
                            'type'=>0,
                            'ctypeid1'=>$value['tag_id'],
                            'ctypeid2'=>$v['cid2'],
                            'discount_rate'=>1,
                        ];
                        db("tob_discount")->insert($insertData);
                        $lastid = db('tob_discount')->getLastInsID();
                        $insertData['id'] = $lastid;
                        $info[] = $insertData;
                    }
                }
            }
        }

        $standard_info = db("tob_discount")
            ->where("type",1)
            ->order("id desc")
            ->select();

        $this->assign('info', $info);
        $this->assign('standard_info', $standard_info);
        return $this->fetch();
    }


    public function add_discount()
    {
        $model = new TobDiscountModel();
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id > 0) {
            //是修改操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $id = $post['id'];
                $standIdStr = $post['standIdStr'];
                $discount_rate = $post['discount_rate'];
                $titleArr = [];
                if(!empty($standIdStr) && $standIdStr!==''){
                    $standIdArr = explode(",",$standIdStr);
                    foreach ($standIdArr as $key=>$value) {
                        $standard = db("standard")->where("id",$value)->field("id,name")->find();
                        $titleArr[] = $standard['id']."_".$standard['name'];
                    }
                }
                $title = '';
                if(!empty($titleArr)){
                    $title = implode("，<br>",$titleArr);
                }

                $updateData = [
                    'title'=>$title,
                    'discount_rate'=>$discount_rate,
                    'ids_str'=>$standIdStr,
                ];

                if (false == $model->allowField(true)->save($updateData, ['id' => $id])) {
                    return $this->error('修改折扣规则失败');
                } else {
                    return $this->success('修改折扣规则成功');
                }
            }else{
                $where['id'] = $id;
                $info = $model->where($where)->find();
                $standardsArr = [];
                if(!empty($info['ids_str'])){
                    $ids_arr = explode(",",$info['ids_str']);
                    foreach ($ids_arr as $key=>$value) {
                        $sdata = db("standard")->where("id",$value)->field("id,name")->find();
                        $standardsArr[] = [
                            "id"=>$value,
                            "name"=>$sdata['name']
                        ];
                    }
                    $this->assign('standardsArr', $standardsArr);
                }

                $this->assign('info', $info);
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $standIdStr = $post['standIdStr'];
                $discount_rate = $post['discount_rate'];

                $titleArr = [];
                if(!empty($standIdStr) && $standIdStr!==''){
                    $standIdArr = explode(",",$standIdStr);
                    foreach ($standIdArr as $key=>$value) {
                        $standard = db("standard")->where("id",$value)->field("id,name")->find();
                        $titleArr[] = $standard['id']."_".$standard['name'];
                    }
                }
                $title = '';
                if(!empty($titleArr)){
                    $title = implode("，<br>",$titleArr);
                }

                $insertData = [
                    'title'=>$title,
                    'discount_rate'=>$discount_rate,
                    'ids_str'=>$standIdStr,
                ];

                if (false == $model->allowField(true)->save($insertData)) {
                    return $this->error('添加折扣规则失败');
                } else {
                    return $this->success('添加折扣规则成功');
                }
            }
        }

        $standardData = db("standard")->select();
        $this->assign('standardData', $standardData);
        return $this->fetch();
    }


    /**
     * 设置折扣率
     */
    public function set_discount()
    {
        if($this->request->isPost()) {
            $post = $this->request->post();
            if(empty($post['id']) || empty($post['discount'])){
                return $this->error("折扣值设置错误");
            }
            $id = $post['id'];
            $discount = $post['discount'];

            $TobDiscountModel = new TobDiscountModel();
            if($discount>=0.1 && $discount<=1) {
                if(false!==$TobDiscountModel->allowField(true)->save(['discount_rate' => $discount],['id'=>$id])){
                    return $this->success("设置成功");
                }
            }else{
                return $this->error("折扣值请设置在0.1-1范围内2位小数");
            }
        }else{
            return $this->error("设置失败");
        }
    }


    //删除
    public function del_discount(){
        if($this->request->isPost()) {
            $post = $this->request->post();
            if(empty($post['id'])){
                return $this->error("id错误");
            }
            $id = $post['id'];
            $TobDiscountModel = new TobDiscountModel();
            if(false!==$TobDiscountModel->where("id",$id)->delete()){
                return $this->success("删除成功");
            }else{
                return $this->error("删除失败");
            }
        }else{
            return $this->error("post请求错误");
        }
    }



}